import 'package:flutter/material.dart';
import 'package:hkzf_flutter/config.dart';
import 'package:hkzf_flutter/dio/user_info.dart';
import 'package:hkzf_flutter/scoped_model/auth_model.dart';
import 'package:hkzf_flutter/utils/scoped_model_helper.dart';
import 'package:hkzf_flutter/widgets/common_image.dart';
import 'package:scoped_model/scoped_model.dart';

const txtStyle = TextStyle(
  fontSize: 20.0,
  color: Colors.white,
  fontWeight: FontWeight.bold,
);

// 个人中心头部组件
class ProfileHeader extends StatelessWidget {
  const ProfileHeader({super.key});

// 未登录用户视图
  Widget _notLoginBuilder(BuildContext context){
    return  Container(
      padding: const EdgeInsets.only(top: 10.0,left: 20.0,bottom: 20.0),
      decoration: const BoxDecoration(color: Colors.green),
      height: 95.0,
      child: Row(
        children: [
          Container(
              height: 65.0,
              width: 65.0,
              margin: const EdgeInsets.only(right: 15.0),
              child: const CircleAvatar(
                backgroundImage:
                    AssetImage('static/images/home_profile_user.png'),
              )),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                children: [
                  GestureDetector(
                    onTap: () {
                      Navigator.pushNamed(context, '/login');
                    },
                    child: const Text('登录',style: txtStyle,),
                  ),
                  const Text('/',style: txtStyle,),
                  GestureDetector(
                    onTap: () {
                      Navigator.pushNamed(context, '/register');
                    },
                    child: const Text('注册',style: txtStyle,),
                  )
                ],
              ),
              const Text('登录后可以体验更多',style: TextStyle(color: Colors.white),),
            ],
          )
        ],
      ),
    );
  }

  // 已登录用户视图
  Widget _loginBuilder(BuildContext context){
    // 从状态管理中获取用户信息
    UserInfo? userInfo = ScopedModel.of<AuthModel>(context).userInfo;
    String userName = userInfo?.nickname ??'测试用户';
    String userAvatar =userInfo?.avatar ?? 'https://picsum.photos/200/200?random=${DateTime.now().millisecondsSinceEpoch}';
    // 如果用户头像不是以http开头，拼接图片地址
    if(!userAvatar.startsWith('http')){
      userAvatar = Config.BaseUrl + userAvatar;
    }
    return Container(
      padding: const EdgeInsets.only(top: 10.0,left: 20.0,bottom: 20.0),
      decoration: const BoxDecoration(color: Colors.green),
      height: 95.0,
      child: Row(
        children: [
          Container(
              height: 65.0,
              width: 65.0,
              margin: const EdgeInsets.only(right: 15.0),
              child: CircleAvatar(
                backgroundImage:
                    NetworkImage(userAvatar),
              )),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(userName,style: txtStyle,),
              const Text('查看编辑个人资料',style: TextStyle(color: Colors.white),),
            ],
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    // 从model获取是否登录
    var isLogin = ScopedModelHelper.getModel<AuthModel>(context).isLogin;
    return isLogin ? _loginBuilder(context): _notLoginBuilder(context);
  }
}
